// 未完成
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int n, a[N];
int dp[N][4][4][2];

/*
将 dp 数组从五维降为四维：dp[i][j][k][p]
i: 当前天数
j: 最近一次出口的水果品种
k: 倒数第二次出口的水果品种
p: 出口地（0 表示南极，1 表示北极）
*/

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];

  memset(dp, -0x3f, sizeof(dp));
  dp[0][0][0][0] = dp[0][0][0][1] = 0;

  for (int i = 1; i <= n; i++) {
    for (int j = 0; j <= 3; j++) {
      for (int k = 0; k <= 3; k++) {
        // Not exporting this fruit
        dp[i][j][k][0] = max(dp[i - 1][j][k][0], dp[i - 1][j][k][1]);
        dp[i][j][k][1] = max(dp[i - 1][j][k][0], dp[i - 1][j][k][1]);

        if (j > 0) {
          for (int l = 0; l <= 3; l++) {
            int score = 1 + (a[i] != j) + (a[i] != k && j != k);
            // Export to South Pole
            dp[i][a[i]][j][0] =
                max(dp[i][a[i]][j][0], dp[i - 1][j][k][0] + score);
            // Export to North Pole
            dp[i][a[i]][j][1] =
                max(dp[i][a[i]][j][1], dp[i - 1][j][k][1] + score);
          }
        }
      }
    }
  }

  int ans = 0;
  for (int j = 0; j <= 3; j++) {
    for (int k = 0; k <= 3; k++) {
      ans = max({ans, dp[n][j][k][0], dp[n][j][k][1]});
    }
  }

  cout << ans << endl;
  return 0;
}